import { PeerToPeer } from "../../dist/esm/index.js";
const send2Worker = document.querySelector("#send2Worker");
const invokeWorker = document.querySelector("#invokeWorker");

const worker1 = new Worker("./worker1.js", { type: "module" });
const worker2 = new Worker("./worker2.js", { type: "module" });
export const init = async () => {
  const peer = new PeerToPeer([worker1, worker2], {
    handlers: {
      // 父页面的处理函数
      log: console.log,
    },
  });
  peer.connect();
  peer.server.on("msg", console.log.bind(null, "parent收到消息"));
  send2Worker.addEventListener("click", () => {
    peer.broadcast({ type: "msg", data: { name: "parent" } });
  });
  invokeWorker.addEventListener("click", () => {
    peer.invoke({ type: "worker1info", data: ["parent"] });
    peer.invoke({ type: "worker2info", data: ["parent"] });
  });
};
